using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GUIPanelInventory : GUIPanelWithButtons {

	public List<GameObject> slots;
	public GameObject slotPrefab;
	public List<GUIButton> hotskillsButtons;
	public GameObject[] hotSkillsVisuals;
	
	private Vector3 slotsOffset;
	private Vector3 slotsScaleFactor;
	private string itemToolTip;
	private Vector3 hotSkillScale;
	
	private Camera GUICamera;
	
	public override void SendClic (Vector3 position)
	{
		foreach (GUIButton hotButton in hotskillsButtons){
			//Debug.Log("clic Position " + clicPosition);
			if (hotButton.isPositionInsideButton(position) ){
				Debug.Log("Click sobre un hotskill");
				if (hotButton.enabled)
					hotButton.OnClickEvent(position);
				
				Item itemDragged = InventoryController.GetInstance()
				.GetActiveInventory().itemBeingDragged;
				if (itemDragged != null){
					InventoryController.GetInstance().GetActiveInventory()
					.ReturnItemToOriginalPosition(itemDragged);
				}
			}
		}
		//base.SendClic (position);
		GUIButton button = GetSlotByClicPosition(position);
		if (button != null)//Click fuera del inventario.
		InventoryController.GetInstance().GetActiveInventory().OnClic(
			button.GetComponent<InventorySlot>().GetGridIndex(0),
			button.GetComponent<InventorySlot>().GetGridIndex(1) );
	}
	public override void SendRightClic (Vector3 position)
	{
		//base.SendClic (position);
		GUIButton button = GetSlotByClicPosition(position);
		if (button == null)
			return;
		Item itemInSlot = 
			InventoryController.GetInstance().GetActiveInventory().GetItemInSlot
			(button.GetComponent<InventorySlot>().GetGridIndex(0), 
			 button.GetComponent<InventorySlot>().GetGridIndex(1) );
		if (itemInSlot != null)
			itemInSlot.Use();
	}
	void Awake(){
		slotsScaleFactor = new Vector3 (8.9f, 11.1f, 0);
		GUICamera = this.transform.parent.GetComponent<Camera>();
		hotSkillsVisuals = new GameObject[3];
		hotSkillScale = new Vector3(2,2,0.1f);
	}
	void Update(){
		
		//Fills Item Tooltip.
		UpdateTooltip();
	}
	private void UpdateTooltip(){
		Ray mousePosition = GUICamera.ScreenPointToRay(Input.mousePosition);	
		RaycastHit raycastHitInfo;
				
		if (Physics.Raycast(mousePosition, out raycastHitInfo)){
			GUIPanelInventory panel = 
				raycastHitInfo.collider.GetComponent<GUIPanelInventory>();
			if (panel != null){
				//Debug.Log("PANEL");
				GUIButton slot = panel.GetSlotByClicPosition(raycastHitInfo.point);
				if (slot != null){
					//Debug.Log("SLOT");
					Item item = 
					InventoryController.GetInstance().GetActiveInventory().GetItemInSlot(
						slot.GetComponent<InventorySlot>().GetGridIndex(0), 
						slot.GetComponent<InventorySlot>().GetGridIndex(1) );
					if (item != null && !item.isBeingDragged){
						//Debug.Log("Estas sobre un item.");
                        MouseController.instance.drawCursor = false;
						itemToolTip = item.GetDescription();
						return;
					}
				}
			}
		}
        if(InventoryController.GetInstance().GetActiveInventory().itemBeingDragged == null)
        MouseController.instance.drawCursor = true;
		itemToolTip = null;
		
	}
	GUIButton GetSlotByClicPosition(Vector3 clicPosition){
		foreach (GUIButton button in buttons){
		//	Debug.Log("clic Position " + clicPosition);
			if (button.isPositionInsideButton(clicPosition) ){
				/*Debug.Log("Click sobre el slot: " 
					+ button.GetComponent<InventorySlot>().GetGridIndex(0) + "," 
					+ button.GetComponent<InventorySlot>().GetGridIndex(1) 
					+ "Item guardado: " 
					+ InventoryController.GetInstance().GetActiveInventory().GetItemInSlot(
						button.GetComponent<InventorySlot>().GetGridIndex(0), 
						button.GetComponent<InventorySlot>().GetGridIndex(1) ) );*/
				//button.OnClickEvent(clicPosition);
				
			/*	InventoryController.GetInstance().GetActiveInventory().OnClic(
					button.GetComponent<InventorySlot>().GetGridIndex(0),
					button.GetComponent<InventorySlot>().GetGridIndex(1) );
			*/	return button;
			}
		}
		return null;
	}
	public void DrawSlots(Inventory inv){
        //esto deberia ser miembro en algun lado
		slotsOffset = this.transform.position;
		slotsOffset.x -= this.transform.localScale.x/2;
		slotsOffset.y += this.transform.localScale.y/1.6f;
		
		for (int row=0; row<inv.GetRowsCount(); row++){
			for (int col=0; col<inv.GetColumnsCount(); col++){
				GameObject go = (GameObject)GameObject.Instantiate(slotPrefab);	
				go.transform.position = slotsOffset;
				//Debug.LogWarning("scale: " + go.transform.localScale);
				go.transform.localScale = ResizeScaleToFitPanel(go.transform.localScale);
				
				go.transform.position += Vector3.right*col*go.transform.localScale.x 
										+ Vector3.down*row*go.transform.localScale.y;
				go.transform.parent = this.transform;
				go.GetComponent<InventorySlot>().SetGridIndex(row,col);
				buttons.Add(go.GetComponent<InventorySlot>() );
				slots.Add(go);
				inv.SetSlotVisual(row,col,go);
				go.SetActive(false);
			}
		}
		DrawHotSkills();
	}
	public void DrawHotSkills(){
		CharacterData cd = 
			CharacterDataManager.GetInstance().GetCurrentCharData();
		for (int i=0; i<3; i++){
			if (hotSkillsVisuals[i] != null)
				GameObject.Destroy(hotSkillsVisuals[i]);
			
			if (cd.hotSkills[i] != null){
				hotSkillsVisuals[i] = GameObject.Instantiate(
				(Object)cd.hotSkills[i].visualModelGUI) as GameObject; 
				
				hotSkillsVisuals[i].transform.position = 
					hotskillsButtons[i].transform.position - Vector3.forward;
				
				Vector3 result = Vector3.zero;
				result.x = this.transform.localScale.x * 5/20;
				result.y = this.transform.localScale.y * 5/25;
				result.z = 1;
				hotSkillsVisuals[i].transform.localScale = result;
					 
			}
		}
	}
	public void ClearSlots(){
		buttons.Clear();
		foreach(GameObject go in slots){
			GameObject.Destroy(go);	
		}
		slots.Clear ();
	}
	public Vector3 ResizeScaleToFitPanel(Vector3 scale){
		//Debug.LogWarning("slotscale: " + slotsScaleFactor);
		Vector3 result = Vector3.zero;
		
		result.x = transform.localScale.x / slotsScaleFactor.x;
		result.y = transform.localScale.y / slotsScaleFactor.y;
		result.z = 0;
		//Debug.LogWarning("resultscale: " +result);
		result.x *= scale.x;
		result.y *= scale.y;
		result.z *= scale.z;
		//Debug.LogWarning("resultscale: " +result);
		return result;
	}
	
	void OnGUI(){
		if (itemToolTip != null){
			GUI.TextField(new Rect(
				Input.mousePosition.x + 30,//Screen.width/2 - textFieldWidth/2, 
				(Screen.height - Input.mousePosition.y),//Screen.height/2 - textFieldHeight/2,
				350,//textFieldWidth, 
				150),//textFieldHeight), 
				itemToolTip);
		}
	}
}
